package test.json;

import net.wanderinghorse.json.JSONException;
import net.wanderinghorse.json.JSONObject;
import net.wanderinghorse.jsonmessage.*;
import net.wanderinghorse.jsonmessage.poster.LocalRequestPoster;

import java.io.PrintStream;
import java.net.URL;
import java.util.Date;
import java.lang.String;

public class AncientTester
{
    class TestMessage extends JSONMessage
    {
        private static final String msgType = "testMessage";
        public TestMessage()
        {
            //super("testMessage", "TestMessage");
            this.setType( msgType );
        }
        public TestMessage(String json) throws JSONException
        {
            //super("testMessage", "TestMessage");

            this.fromJSON(json);
        }
        public TestMessage(JSONObject json) throws JSONException
        {
            //super("testMessage", "TestMessage");
            this.fromJSONObject(json);
        }
    }
    

        // Tester constants.

        //--------------------------------------------------------------------------

        // Tester variables.

        //--------------------------------------------------------------------------

    private void testFromJSON( java.io.PrintStream out ) throws JSONException
    {
        JSONObject jo = new JSONObject();
        JSONMessage m = new TestMessage();
        jo.put( "now", (new Date()).getTime() );
        m.setPayload(jo);
        String json = m.toJSON();
        out.println(json);
        //m.setType(null);
        //jo.put( "type", "FooMessage");
        m.setWrapperName( "FooMessage" );
        json = m.toJSON();
        out.println(json);
        jo = (new JSONMessage(json)).toJSONObject();
        json = jo.toString();
        out.println(json);
    }

    private void testAliaser( PrintStream out )
    {
        Aliaser a = new Aliaser();
        a.alias("foo","bar.foo");
        a.alias("bar.foo","Bar.Foo");
        out.println( a.resolve("foo") );
    }
    /** Tester constructor. */
    public AncientTester()
    {
        super();
        java.io.PrintStream out = System.out;
        out.println("Tester Application");
        try
        {
            out.println( (new TestMessage()).toJSON() );
            JSONMessage m = new TestMessage();

            JSONObject jo = new JSONObject();
            jo.put("yo","hi");
            jo.put("date", new JSONObject(new Date(m.getTimestamp())) );
            m.setPayload( jo );
            jo = new JSONObject();
            jo.put("version","0.0.1-alphalpha");
            m.setOptions( jo );
            m.setCredentials("bob","dylan");
            out.println(m.toJSON());
            JSONMessage clone = new TestMessage( m.toJSON() );
            out.println(clone.toJSON());
            this.testFromJSON(out);
            this.testAliaser(out);

            RequestPoster poster = new LocalRequestPoster();
            Request req = new Request(clone.toJSONObject());

            {
//                ClassLoader cl =
//                        //this.getClass().getClassLoader()
//                        Thread.currentThread().getContextClassLoader()
//                        ;
                URL u =
                        //getClass().getClassLoader()
                        getClass()
                        //Thread.currentThread().getContextClassLoader()
                        .getResource( JSONMessage.CONFIG_FILE);
                out.println( "Config URL: "+u);

            }

            {
                out.println("PostOptions.createDefault():");
                out.println(PostOptions.createDefault().toJSON());
            }

            RequestHandler onResponse = new RequestHandler() {
                    public void processResponse(Request _req, Response resp) {
                        PrintStream ps = System.out;
                        try {
                            final int indent = 4;
                            ps.println("onResponse() handler:");
                            ps.println("REQUEST:"+_req.toJSON(indent));
                            ps.println("RESPONSE:"+resp.toJSON(indent));
                        }
                        catch(/*JSONException */Exception jex) {
                            ps.println("EXCEPTION: "+jex);
                        }
                    }
            };
            {
                req = new Request();
                req.setType("ChuckNorris");
                RequestHandler hnd = RequestHandlerLoader.instance().loadRequestHandler( req );
                Response res = new Response();
                hnd.processResponse(req, res);
                out.println("ResponseHandler says:\n"+res.toJSON());
            }

        }
        catch(Exception e)
        {
            out.println("EXCEPTION: "+e);
        }
    }

        //--------------------------------------------------------------------------

    /** Tester main method.
     * @param arguments Program args (ignored).
     * */
    public static void main(String[] arguments)
    {
                // Create Tester.
        new AncientTester();
    }
}
